package cn.gy.productclient.client;

import cn.gy.common.model.Product;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@FeignClient(name = "product-service")
public interface ProductClient {
    static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ProductClient.class);

    /**
     * 根据商品id获取商品对象
     */
    @GetMapping("/findByProductId/{productId}")
    @RateLimiter(name = "ratelimiterApi", fallbackMethod = "ratelimiterFallback")
    Product findByProductId(@RequestParam(value = "productId") Long productId);

    /**
     * 根据商品名称获取商品对象
     */
    @GetMapping("/findByProductName/{productName}")
    @Retry(name = "retryApi", fallbackMethod = "retryApiFallback")
    Product findByProductName(@RequestParam(value = "productName") String productName);

    /**
     * 获取所有商品集合
     */
    @GetMapping("queryAllProduct")
    @CircuitBreaker(name = "breakerApi", fallbackMethod = "breakerFallback")
    List<Product> queryAllProduct();

    default List<Product> breakerFallback(Throwable throwable) {
        log.info("--------CircuitBreaker fallback triggered: --------" + throwable.getMessage());
        return null;
    }

    default public Product ratelimiterFallback(Long productId, Throwable throwable) {
        log.info("--------ratelimiterFallback triggered: --------" + throwable.getMessage());
        return null;
    }

    default public Product retryApiFallback(String nm, Throwable throwable) {
        log.info("--------retryApiFallback triggered: --------" + throwable.getMessage());
        return null;
    }
}
